package com.cat.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cat.dto.PageDTO;
import com.cat.entity.Cat;
import com.cat.mapper.CatMapper;
import com.cat.query.CatQuery;
import com.cat.service.ICatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cat.vo.CatVO;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lmy
 * @since 2024-09-26
 */
@Service
public class CatServiceImpl extends ServiceImpl<CatMapper, Cat> implements ICatService {

    @Override
    public PageDTO<CatVO> getByCatList(CatQuery catQuery) {
        Page<Cat> catPage = new Page<>(catQuery.getPageNum(), catQuery.getPageSize());

        Page<Cat> page = lambdaQuery()
                .eq(catQuery.getStatus() != null,Cat::getStatus,catQuery.getStatus())
                .like(catQuery.getName() != null , Cat::getName,catQuery.getName())
                .eq(catQuery.getGender() != null, Cat::getGender,catQuery.getGender())
                .eq(catQuery.getVaccinated() != null, Cat::getVaccinated,catQuery.getVaccinated())
                .eq(catQuery.getSterilized() != null, Cat::getSterilized,catQuery.getSterilized())
                .eq(catQuery.getBreed() != null, Cat::getBreed,catQuery.getBreed())
                .page(catPage);
        PageDTO<CatVO> catVOPageDTO = new PageDTO<>();
        catVOPageDTO.setPageSize(page.getSize());
        catVOPageDTO.setPageNum(page.getCurrent());
        catVOPageDTO.setTotal(page.getTotal());

        List<Cat> records = page.getRecords();
        if(CollUtil.isEmpty(records)){
            // Collections.emptyList() 返回一个空列表
            catVOPageDTO.setList(Collections.emptyList());
        } else {
            List<CatVO> catVOS = BeanUtil.copyToList(records, CatVO.class);
            catVOPageDTO.setList(catVOS);
        }

        return catVOPageDTO;
    }
}
